﻿using System;
using System.Web.Mvc;

namespace Agile.Redsys.Web.Mvc
{
    /// <summary>
    /// Notification sent by Redsys to notify new subscriptions or sales.
    /// </summary>
    [ModelBinder(typeof(RedsysNotificationModelBinder))]
    public class RedsysNotification {
        /// <summary>
        /// Initializes a new instance of the <see cref="RedsysNotification"/> class.
        /// </summary>
        /// <param name="merchantData">The merchant data.</param>
        /// <param name="amount">The amount.</param>
        /// <param name="order">The order.</param>
        /// <param name="response">The response.</param>
        /// <param name="authorizationCode">The authorization code.</param>
        /// <param name="consumerLanguage">The consumer language.</param>
        /// <param name="cardType">Type of the card.</param>
        /// <param name="date">The date.</param>
        /// <param name="merchantCode">The merchant code.</param>
        /// <param name="terminal">The terminal.</param>
        /// <param name="signature">The signature.</param>
        /// <param name="securePayment">if set to <c>true</c> [secure payment].</param>
        /// <param name="transactionType">Type of the transaction.</param>
        /// <param name="cardCountry">The card country.</param>
        /// <param name="currency">The currency.</param>
        public RedsysNotification(string merchantData, decimal amount, string order, int response, string authorizationCode, string consumerLanguage, string cardType, DateTime date,  string merchantCode, int terminal, string signature, bool securePayment, RedsysTransactionType transactionType, string cardCountry, string currency)
        {
            MerchantData = merchantData;
            Amount = amount;
            Order = order;
            Response = response;
            AuthorizationCode = authorizationCode;
            ConsumerLanguage = consumerLanguage;
            CardType = cardType;
            DateTime = date;
            MerchantCode = merchantCode;
            Terminal = terminal;
            Signature = signature;
            SecurePayment = securePayment;
            TransactionType = transactionType;
            CardCountry = cardCountry;
            Currency = currency;
        }

        /// <summary>
        /// Gets the merchant data.
        /// </summary>
        /// <value>The merchant data.</value>
        public string MerchantData { get; private set; }
        /// <summary>
        /// Gets the amount.
        /// </summary>
        /// <value>The amount.</value>
        public decimal Amount { get; private set; }
        /// <summary>
        /// Gets the order.
        /// </summary>
        /// <value>The order.</value>
        public string Order { get; private set; }
        /// <summary>
        /// Gets the response.
        /// </summary>
        /// <value>The response.</value>
        public int Response { get; private set; }
        /// <summary>
        /// Gets the authorization code.
        /// </summary>
        /// <value>The authorization code.</value>
        public string AuthorizationCode { get; private set; }
        /// <summary>
        /// Gets the consumer language.
        /// </summary>
        /// <value>The consumer language.</value>
        public string ConsumerLanguage { get; private set; }
        /// <summary>
        /// Gets the type of the card.
        /// </summary>
        /// <value>The type of the card.</value>
        public string CardType { get; private set; }
        /// <summary>
        /// Gets the date time.
        /// </summary>
        /// <value>The date time.</value>
        public DateTime DateTime { get; private set; }
        /// <summary>
        /// Gets the merchant code.
        /// </summary>
        /// <value>The merchant code.</value>
        public string MerchantCode { get; private set; }
        /// <summary>
        /// Gets the terminal.
        /// </summary>
        /// <value>The terminal.</value>
        public int Terminal { get; private set; }
        /// <summary>
        /// Gets the signature.
        /// </summary>
        /// <value>The signature.</value>
        public string Signature { get; private set; }
        /// <summary>
        /// Gets a value indicating whether the payment was secure.
        /// </summary>
        /// <value><c>true</c> if secure payment; otherwise, <c>false</c>.</value>
        public bool SecurePayment { get; private set; }
        /// <summary>
        /// Gets the type of the transaction.
        /// </summary>
        /// <value>The type of the transaction.</value>
        public RedsysTransactionType TransactionType { get; private set; }
        /// <summary>
        /// Gets the card country.
        /// </summary>
        /// <value>The card country.</value>
        public string CardCountry { get; private set; }
        /// <summary>
        /// Gets the currency.
        /// </summary>
        /// <value>The currency.</value>
        public string Currency { get; private set; }

        /// <summary>
        /// Determines whether the notification is valid.
        /// </summary>
        /// <param name="secret">The secret.</param>
        /// <returns><c>true</c> if the notification is valid; otherwise, <c>false</c>.</returns>
        public bool IsValid(string secret)
        {

            return SignatureHelper.IsNotificationValid(Amount, Order, MerchantCode, Convert.ToInt32(Currency), Response, secret, Signature);

        }

   
    }
}